﻿using dbe.FoundationLibrary.Core.Win32.Structures;

using System;
using System.Runtime.InteropServices;
using System.Text;

namespace dbe.FoundationLibrary.Core.Win32.API
{
    /// <summary>
    /// <c>"user32.dll"</c>中的Api
    /// </summary>
    public class User32
    {
        /// <summary>
        /// 用于在菜单中添加菜单项。它通常用于自定义菜单，例如在应用程序中添加新的菜单项。
        /// </summary>
        /// <example>
        /// 以下是一个示例，演示如何直接使用gdi32.dll中的SelectObject函数：
        /// <code lang="cs">
        /// <![CDATA[
        ///     IntPtr hWnd = new IntPtr(1); // 示例窗口句柄
        ///     IntPtr hMenu = GetMenu(hWnd);
        ///     
        ///     AppendMenu(hMenu, 0, 1, "我的菜单项");
        /// ]]>
        /// </code>
        /// </example>
        /// <param name="hMenu">菜单指针</param>
        /// <param name="uFlags"></param>
        /// <param name="uIDNewItem"></param>
        /// <param name="lpNewItem"></param>
        /// <returns></returns>
        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        public static extern bool AppendMenu(IntPtr hMenu, int uFlags, int uIDNewItem, string lpNewItem);

        /// <summary>
        /// 用于将钩子消息传递给下一个钩子。钩子是一种特殊类型的函数，可以拦截 Windows 消息，以便在消息发送到目标窗口之前对其进行处理。
        /// </summary>
        /// <param name="idHook">要构的对象的句柄</param>
        /// <param name="nCode">钩子消息的代码</param>
        /// <param name="wParam"></param>
        /// <param name="lParam"></param>
        /// <returns></returns>
        [DllImport("user32.dll", SetLastError = true)]
        public static extern IntPtr CallNextHookEx(IntPtr idHook, int nCode, IntPtr wParam, IntPtr lParam);

        [DllImport("user32.dll")]
        public static extern int ChangeDisplaySettings(ref _devicemode devMode, int flags);

        /// <summary>
        /// 客户区坐标转屏幕坐标
        /// </summary>
        /// <param name="hWnd">handle to window</param>
        /// <param name="lpPoint">control coordinates</param>
        /// <returns></returns>
        [DllImport("User32.dll")]
        public static extern bool ClientToScreen(IntPtr hWnd, ref tagPOINT lpPoint);

        [DllImport("user32.dll")]
        public static extern int EndDialog(IntPtr hDlg, IntPtr nResult);

        [DllImport("user32.dll")]
        public static extern bool EnumChildWindows(IntPtr hWndParent, EnumChildProc lpEnumFunc, IntPtr lParam);

        [DllImport("user32.dll")]
        public static extern int EnumDisplaySettings(string deviceName, int modeNum, ref _devicemode devMode);

        /// <summary>
        /// 使用指定的画刷（brush）填充给定的矩形区域。
        /// </summary>
        /// <param name="hDC">设备上下文句柄。</param>
        /// <param name="lprc">
        /// 指向一个RECT结构体的指针，该结构体定义了要填充的矩形区域的坐标。RECT结构体是一个包含矩形左、上、右、下四个顶点坐标的结构体。
        /// </param>
        /// <param name="hbr">画刷句柄。</param>
        /// <returns>
        /// 如果函数成功执行，返回值为非零值。
        /// 如果函数执行失败，返回0。
        /// </returns>
        [DllImport("user32.dll")]
        public static extern int FillRect(IntPtr hDC, ref tagRECT lprc, IntPtr hbr);

        [DllImport("user32.dll")]
        public static extern int FindWindow(string lpClassName, string lpWindowName);

        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern int GetClassLong(IntPtr hwnd, int nIndex);

        [DllImport("user32.dll", EntryPoint = "GetClassNameW", CharSet = CharSet.Unicode)]
        public static extern int GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount);

        /// <summary>
        /// 用于获取设备上下文（Device Context）的句柄。设备上下文是一个与设备相关的数据结构，包含了设备的信息和状态。
        /// </summary>
        /// <example>
        /// 以下是一个示例，演示如何直接使用user32.dll中的SelectObject函数：
        /// <code lang="cpp">
        /// <![CDATA[
        ///     IntPtr hWnd = new IntPtr(1); // 示例窗口句柄
        ///     IntPtr hDC = GetDC(hWnd);
        ///     
        ///     ...
        /// ]]>
        /// </code>
        /// </example>
        /// <param name="hWnd">窗口的设备上下文句柄</param>
        /// <returns></returns>
        [DllImport("user32.dll", SetLastError = true)]
        public static extern IntPtr GetDC(IntPtr hWnd);

        [DllImport("user32.dll")]
        public static extern IntPtr GetDesktopWindow();

        [DllImport("user32.dll")]
        public static extern int GetDlgCtrlID(IntPtr hwndCtl);

        [DllImport("user32.dll")]
        public static extern IntPtr GetDlgItem(IntPtr hDlg, int nIDDlgItem);

        /// <summary>
        /// 获取系统菜单
        /// </summary>
        /// <param name="hWnd"></param>
        /// <param name="bRevert"></param>
        /// <returns></returns>
        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        public static extern IntPtr GetSystemMenu(IntPtr hWnd, bool bRevert);

        [DllImport("user32.dll")]
        public static extern IntPtr GetWindowDC(IntPtr ptr);

        /// <summary>
        /// 获取指定句柄控件的Text属性值
        /// </summary>
        /// <param name="hWnd"></param>
        /// <param name="text"></param>
        /// <param name="maxLength"></param>
        /// <returns></returns>
        [DllImport("user32.dll", EntryPoint = "GetWindowTextW", CharSet = CharSet.Unicode)]
        public static extern int GetWindowText(IntPtr hWnd, StringBuilder text, int maxLength);

        [DllImport("user32.dll", EntryPoint = "GetWindowTextLengthW", CharSet = CharSet.Unicode)]
        public static extern int GetWindowTextLength(IntPtr hWnd);

        /// <summary>
        /// 插入菜单项到窗口自带的菜单
        /// </summary>
        /// <param name="hMenu"></param>
        /// <param name="uPosition"></param>
        /// <param name="uFlags"></param>
        /// <param name="uIDNewItem"></param>
        /// <param name="lpNewItem"></param>
        /// <returns></returns>
        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        public static extern bool InsertMenu(IntPtr hMenu, int uPosition, int uFlags, int uIDNewItem, string lpNewItem);

        [DllImport("user32.dll")]
        public static extern bool PrintWindow(IntPtr hwnd, IntPtr hdcBlt, UInt32 nFlags);

        [DllImport("user32.dll")]
        public static extern bool ReleaseCapture();

        /// <summary>
        /// 释放设备上下文句柄。
        /// </summary>
        /// <example>
        /// 以下是一个示例，演示如何直接使用gdi32.dll中的SelectObject函数：
        /// <code lang="cs">
        /// <![CDATA[
        ///     IntPtr hWnd = new IntPtr(1); // 示例窗口句柄
        ///     IntPtr hDC = GetDC(hWnd);
        ///     
        ///     // ...
        ///     
        ///     int nRet = ReleaseDC(hWnd, hDC);
        /// ]]>
        /// </code>
        /// </example>
        /// <param name="hWnd">窗口句柄</param>
        /// <param name="hdc">设备上下文句柄</param>
        /// <returns>
        /// 返回一个整数值，表示释放的设备上下文句柄的引用计数。
        /// 如果引用计数不为 0，则表示设备上下文句柄尚未释放。
        /// </returns>
        [DllImport("user32.dll", SetLastError = true)]
        public static extern bool ReleaseDC(IntPtr hWnd, IntPtr hdc);

        /// <summary>
        /// 屏幕坐标转客户区坐标
        /// </summary>
        /// <param name="hWnd">handle to window</param>
        /// <param name="lpPoint">screen coordinates</param>
        /// <returns></returns>
        [DllImport("User32.dll")]
        public static extern bool ScreenToClient(IntPtr hWnd, ref tagPOINT lpPoint);

        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern int SetClassLong(IntPtr hwnd, int nIndex, int dwNewLong);

        [DllImport("user32.dll")]
        public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);

        [DllImport("user32.dll")]
        public static extern IntPtr SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hInstance, int threadId);

        [DllImport("user32.dll", EntryPoint = "SetWindowTextW", CharSet = CharSet.Unicode)]
        public static extern bool SetWindowText(IntPtr hWnd, string lpString);

        [DllImport("user32.dll")]
        public static extern int ShowWindow(int hwnd, int nCmdShow);

        [DllImport("user32.dll")]
        public static extern bool SystemParametersInfo(uint uiAction, uint uiParam, uint pvParam, uint fWinIni);

        [DllImport("user32.dll")]
        public static extern int UnhookWindowsHookEx(IntPtr idHook);

        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern short VkKeyScan(char key);
    }
}